/**
 * Project Name:dyrj-admin
 * File Name:MenuServiceImpl.java
 * Package Name:com.dyrj.base.system.menu.service.impl
 * Date:2018年1月13日下午2:14:46
 * Copyright (c) 2018, zhang.xiangyu@foxmail.com All Rights Reserved.
*/
package com.dyrj.base.system.menu.service.impl;

import java.util.*;

import javax.annotation.Resource;

import com.dyrj.base.system.role.model.SysRole;
import com.dyrj.base.system.utils.SystemUtil;
import com.dyrj.constant.SysFrameConst;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import com.dyrj.base.system.menu.dao.SysMenuMapper;
import com.dyrj.base.system.menu.model.SysMenu;
import com.dyrj.base.system.menu.service.MenuService;
import com.dyrj.vo.ResultMsg;

/**   
 * @Title: MenuServiceImpl.java 
 * @Package com.dyrj.base.system.menu.service.impl
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author
 * @date 2018年1月13日 下午2:14:46 
 * @version V1.0   
 */
@Service
public class MenuServiceImpl implements MenuService {
	@Resource
	private SysMenuMapper mapper;

	@Override
	public List<SysMenu> selectAllTreeMenu() {
		List<SysMenu> menus = mapper.selectAllTreeMenu();
		SystemUtil.deleteNullChildren(menus);
		return menus;
	}
	
	@Override
	public SysMenu info(Long id) {
		return mapper.selectByPrimaryKey(id);
	}
	
	@Override
	public ResultMsg add(SysMenu record) {
		if(record.getType() == SysFrameConst.ZERO) {
			record.setPid((long) SysFrameConst.ZERO);
		} else {
			if(record.getHierarchy().intValue() == SysFrameConst.ONE) {
				record.setPid(record.getId());
			}
		}
		record.setId(null);
		record.setCreateTime(new Date());
		mapper.insertSelective(record);
		return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE);
	}
	
	@Override
	public ResultMsg updata(SysMenu record) {
		mapper.updateByPrimaryKeySelective(record);
		return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE);
	}
	
	@Override
	public ResultMsg del(Long id) {
		// 删除菜单
		mapper.deleteByPrimaryKey(id);
		// 删除子菜单
		mapper.deleteByPid(id);
		return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE);
	}

	@Override
	public ResultMsg getAllDirectoryMenu() {
		// SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("sysUser");
		// 獲取session中菜單信息是否存在
		List<SysMenu> menus = (List<SysMenu>) SecurityUtils.getSubject().getSession().getAttribute(SysFrameConst.SYSTEM_USER);
		if(menus == null) {
			// 获取权限信息
			List<SysRole> roles = (List<SysRole>) SecurityUtils.getSubject().getSession().getAttribute(SysFrameConst.SYSTEM_ROLES);
			// 获取菜单列表
			// 获取权限字符串
			StringBuffer perms = new StringBuffer();
			roles.forEach(role -> {
				perms.append(role.getRoleSign());
				perms.append(",");
			});

			menus = mapper.selectAllDirectoryMenu();
			menus = SystemUtil.deleteNotPermisMenu(menus, perms.toString());
			SecurityUtils.getSubject().getSession().setAttribute(SysFrameConst.SYSTEM_MENUS, menus);
		}
		return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, menus);
	}

	@Override
	public ResultMsg table(SysMenu record) {
		// PageInfo<SysMenu> page = PageHelper.startPage(record.getOffset(), record.getLimit()).doSelectPageInfo(() -> mapper.findPageList(record));
		List<SysMenu> menus = mapper.selectAllMenu();
		return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, menus);
	}




}
